import { Model, DataTypes, Optional } from 'sequelize';
import { sequelize } from '../config/database';
import User from './User';
import InterestTag from './InterestTag';

export interface UserInterestTagAttributes {
    id: string;
    userId: string;
    tagId: string;
    createdAt?: Date;
    updatedAt?: Date;
}

export interface UserInterestTagCreationAttributes extends Optional<UserInterestTagAttributes, 'id' | 'createdAt' | 'updatedAt'> { }

class UserInterestTag extends Model<UserInterestTagAttributes, UserInterestTagCreationAttributes> implements UserInterestTagAttributes {
    public id!: string;
    public userId!: string;
    public tagId!: string;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

    public static associate(models: any) {
        UserInterestTag.belongsTo(models.User, {
            foreignKey: 'userId',
            as: 'user'
        });

        UserInterestTag.belongsTo(models.InterestTag, {
            foreignKey: 'tagId',
            as: 'tag'
        });
    }
}

export const initUserInterestTag = (sequelize: any) => {
    UserInterestTag.init(
        {
            id: {
                type: DataTypes.UUID,
                defaultValue: DataTypes.UUIDV4,
                primaryKey: true
            },
            userId: {
                type: DataTypes.UUID,
                allowNull: false,
                references: {
                    model: 'users',
                    key: 'id'
                }
            },
            tagId: {
                type: DataTypes.UUID,
                allowNull: false,
                references: {
                    model: 'interest_tags',
                    key: 'id'
                }
            }
        },
        {
            sequelize,
            modelName: 'UserInterestTag',
            tableName: 'user_interest_tags',
            timestamps: true,
            indexes: [
                {
                    unique: true,
                    fields: ['userId', 'tagId']
                }
            ]
        }
    );
};

export default UserInterestTag; 